package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BookShelf {
 ArrayList<Book> listOfBooks = new ArrayList<Book>();
    Map<Book,Customer> lentBooks = new HashMap<Book, Customer>();
    private final String CHECK_OUT_BOOK_SUCCESSFULLY_MESSAGE = "Thank You! Enjoy The Book";
    private final String BOOK_UNAVAILABLE_MESSAGE = "That Book Is Not Available";
    private final String SUCCESSFUL_BOOK_RETURN = "Thank You For Returning The Book";
    private final String INVALID_BOOK_RETURN = "That Is Not A Valid Book To Return";
    Book book = null;
    Customer customer = null;
    public BookShelf (){
        book = new Book(101,"Head First Java","Kent");
        listOfBooks.add(book);
        book = new Book(102,"Agile","Author");
        listOfBooks.add(book);

        customer = new Customer(1,"Ann","123-1234","pass");
    }
    private Book returnBookObjectFromTitle(String bookTitle){

        for (Book thisBook : listOfBooks) {
            if(thisBook.toString() == bookTitle ){
                book = thisBook;
                return book;
            }
        }
        return null;
    }

    public boolean checkoutBook(String bookTitle,String customerName) {
        book = returnBookObjectFromTitle(bookTitle);
        customer = Customer.returnCustomerObjectGivenCustomerName(customerName);
        if( lentBooks != null && lentBooks.containsKey(book)){

            return false;
        }
        else
        {
            if(lentBooks == null){
                lentBooks = new HashMap<Book,Customer>();
            }
                lentBooks.put(book,customer);
        return true;
        }
    }

    public String successfulBookCheckout(String bookId,String customerName) {
        if(checkoutBook(bookId,customerName)){
            return CHECK_OUT_BOOK_SUCCESSFULLY_MESSAGE;
        }
        else{
            return BOOK_UNAVAILABLE_MESSAGE;
        }

    }
    public boolean returnBook(String bookTitle,String customerName) {
        book = returnBookObjectFromTitle(bookTitle);
        customer = Customer.returnCustomerObjectGivenCustomerName(customerName);
        if(lentBooks != null && lentBooks.containsKey(book)){

            lentBooks.remove(book);
            return true;
        }
        else
        {
            return false;
        }
    }

    public String bookReturnSuccessfulMessage(String bookTitle,String customerName) {
        if(returnBook(bookTitle,customerName)){
            return SUCCESSFUL_BOOK_RETURN;
        }
        else
        {
            return INVALID_BOOK_RETURN;
        }
    }

}
